/* Table C7: Stacked Regression Estimates */

local PATH_TBL = "~/Downloads/tables"


use tbC7_long_data_extract, clear
  
// create covariates 
 //create size 
 egen size = sum(n), by(item_id_2)
 gen lsize = log(size)
 
 // create margin 
 egen xD = sum(n*LEFT), by(item_id_2)
 gen xD_pct = xD/(size) * 100
 gen xR_pct = (size - xD)/(size) * 100
 gen margin =  abs(xD_pct - xR_pct)
 gen uncontest = (margin == 100)
 drop if uncontest == 1

 // create partisan slant 
 egen pslant = wtmean(pid_num), by(item_id_2) weight(n)
 replace pslant = abs(pslant - .5) 

	
// Function to create standardize vars from weighted data 
capture program drop wstd 
program define wstd 
    args v w
    gen _mn = sum(`v'*`w')/sum(`w')
    gen _sd = sqrt(sum(`w'*(`v'-_mn)^2)/sum(`w')*(_N/(_N-1)))
    gen std_`v' = (`v' - _mn[_N])/_sd[_N]
    drop _mn _sd
end

// size
rename lsize x
wstd x n
rename std_x lsize
drop x

// margin
rename margin x
wstd x n
rename std_x margin 
drop x

// pslant 
rename pslant x
wstd x n
rename std_x pslant 
drop x
	
// create interaction terms  	
 gen x_margin = pid_num * margin 
 gen x_lsize = pid_num * lsize  
 gen x_pslant = pid_num * pslant
  
 foreach i in spr lpr snpr lnpr sbm lbm {
   gen x_`i' = pid_num * (`i' == 1)
 }


// model 1 
 areg LEFT x_spr x_lpr x_snpr x_lnpr x_sbm x_lbm x_lbm [fw=n], a(item_id_2) vce(cluster item_id_2)

  local b_1 =  _b[x_spr]
  local b_1_2 =  _b[x_lpr]
  local b_1_3 =  _b[x_snpr]
  local b_1_4 =  _b[x_lnpr]
  local b_1_5 =  _b[x_sbm]
  local b_1_6 =  _b[x_lbm]

  local s_1 =  _se[x_spr]
  local s_1_2 =  _se[x_lpr]
  local s_1_3 =  _se[x_snpr]
  local s_1_4 =  _se[x_lnpr]
  local s_1_5 =  _se[x_sbm]
  local s_1_6 =  _se[x_lbm]
  local n_1 = e(N)

  
// model 2 - p slant  
 areg LEFT x_spr x_lpr x_snpr x_lnpr x_sbm x_lbm x_pslant [fw=n], a(item_id_2) vce(cluster item_id_2)

  local b_2 =  _b[x_spr]
  local b_2_2 =  _b[x_lpr]
  local b_2_3 =  _b[x_snpr]
  local b_2_4 =  _b[x_lnpr]
  local b_2_5 =  _b[x_sbm]
  local b_2_6 =  _b[x_lbm]
  local b_2_7 =  _b[x_pslant]

  local s_2 =  _se[x_spr]
  local s_2_2 =  _se[x_lpr]
  local s_2_3 =  _se[x_snpr]
  local s_2_4 =  _se[x_lnpr]
  local s_2_5 =  _se[x_sbm]
  local s_2_6 =  _se[x_lbm]
  local s_2_7 =  _se[x_pslant]
  local n_2 = e(N)

  
// model 3 - margin  
 areg LEFT x_spr x_lpr x_snpr x_lnpr x_sbm x_lbm x_margin [fw=n], a(item_id_2) vce(cluster item_id_2)

  local b_3 =  _b[x_spr]
  local b_3_2 =  _b[x_lpr]
  local b_3_3 =  _b[x_snpr]
  local b_3_4 =  _b[x_lnpr]
  local b_3_5 =  _b[x_sbm]
  local b_3_6 =  _b[x_lbm]

  local b_3_7 =  _b[x_margin]

  local s_3 =  _se[x_spr]
  local s_3_2 =  _se[x_lpr]
  local s_3_3 =  _se[x_snpr]
  local s_3_4 =  _se[x_lnpr]
  local s_3_5 =  _se[x_sbm]
  local s_3_6 =  _se[x_lbm]
  
  local s_3_7 =  _se[x_margin]
  local n_3 = e(N)
  

// model 4 - race size alone 
 areg LEFT x_spr x_lpr x_snpr x_lnpr x_sbm x_lbm x_lsize [fw=n], a(item_id_2) vce(cluster item_id_2)

  local b_4 =  _b[x_spr]
  local b_4_2 =  _b[x_lpr]
  local b_4_3 =  _b[x_snpr]
  local b_4_4 =  _b[x_lnpr]
  local b_4_5 =  _b[x_sbm]
  local b_4_6 =  _b[x_lbm]
  local b_4_7 =  _b[x_lsize]

  local s_4 =  _se[x_spr]
  local s_4_2 =  _se[x_lpr]
  local s_4_3 =  _se[x_snpr]
  local s_4_4 =  _se[x_lnpr]
  local s_4_5 =  _se[x_sbm]
  local s_4_6 =  _se[x_lbm]
  local s_4_7 =  _se[x_lsize]
  
  local n_4 = e(N)
  
  
// model 5  - all stacked 
  areg LEFT x_spr x_lpr x_snpr x_lnpr x_sbm x_lbm x_pslant x_margin x_lsize [fw=n], a(item_id_2) vce(cluster item_id_2)

  local b_5 =  _b[x_spr]
  local b_5_2 =  _b[x_lpr]
  local b_5_3 =  _b[x_snpr]
  local b_5_4 =  _b[x_lnpr]
  local b_5_5 =  _b[x_sbm]
  local b_5_6 =  _b[x_lbm]
  
  local b_5_7 =  _b[x_pslant]

  local b_5_8 =  _b[x_margin]

  local b_5_9 =  _b[x_lsize]


  local s_5 =  _se[x_spr]
  local s_5_2 =  _se[x_lpr]
  local s_5_3 =  _se[x_snpr]
  local s_5_4 =  _se[x_lnpr]
  local s_5_5 =  _se[x_sbm]
  local s_5_6 =  _se[x_lbm]
  
  local s_5_7 =  _se[x_pslant]
   
  local s_5_8 =  _se[x_margin]

  local s_5_9 =  _se[x_lsize]

  local n_5 = e(N)


  
  
// Table output
set linesize 200

quietly {
    capture log close
    capture log using "`PATH_TBL'/table_C7_full-regressions.tex", replace text

    noisily display "\begin{table}[htpb]"
    noisily display "\centering \small"
    noisily display "\begin{threeparttable}"
    noisily display "\caption{\bf Voting Patterns by Partisanship, Regression Estimates}"
    noisily display "\label{table_stacked-regressions}"

    /* 4 columns: row label + 3 model columns */
    noisily display "\begin{tabular}{l r r r r r}"
    noisily display "\toprule\toprule"
    noisily display "                          &  (1)   & (2)   &  (3)  & (4) & (5) \\ "
    noisily display "\midrule"
    
    // State Partisan Offices
    noisily display "State Partisan Office   & " %5.3f `b_1' "  & " %5.3f `b_2' "  & " %5.3f `b_3' " & " %5.3f `b_4' " & " %5.3f `b_5' " \\"
    noisily display " $\times$ Democrat      & (" %5.3f `s_1' ") & (" %5.3f `s_2' ") & (" %5.3f `s_3' ")  & (" %5.3f `s_4' ") & (" %5.3f `s_5' ") \\\\ [.02in]"
    noisily display "\addlinespace[0.1em]"

    // Local Partisan Offices
    noisily display "Local Partisan Offices   & " %5.3f `b_1_2' "  & " %5.3f `b_2_2' "  & " %5.3f `b_3_2' "  & " %5.3f `b_4_2' " & " %5.3f `b_5_2' " \\"
    noisily display " $\times$ Democrat       & (" %5.3f `s_1_2' ") & (" %5.3f `s_2_2' ") & (" %5.3f `s_3_2' ")  & (" %5.3f `s_4_2' ") & (" %5.3f `s_5_2' ") \\\\ [.02in]"
    noisily display "\addlinespace[0.1em]"

    // State Ideological Measures
    noisily display "State Non-partisan Offices  & " %5.3f `b_1_3' "  & " %5.3f `b_2_3' "  & " %5.3f `b_3_3' " & " %5.3f `b_4_3' " & " %5.3f `b_5_3' " \\"
    noisily display " $\times$ Democrat          & (" %5.3f `s_1_3' ") & (" %5.3f `s_2_3' ") & (" %5.3f `s_3_3' ") & (" %5.3f `s_4_3' ") & (" %5.3f `s_5_3' ") \\\\ [.02in]"
    noisily display "\addlinespace[0.1em]"

    // State Spending Measures
    noisily display "Local Non-partisan Offices  & " %5.3f `b_1_4' "  & " %5.3f `b_2_4' "  & " %5.3f `b_3_4' " & " %5.3f `b_4_4' " & " %5.3f `b_5_4' " \\"
    noisily display " $\times$ Democrat       & (" %5.3f `s_1_4' ") & (" %5.3f `s_2_4' ") & (" %5.3f `s_3_4' ") & (" %5.3f `s_4_4' ")  & (" %5.3f `s_5_4' ") \\\\ [.02in]"
    noisily display "\addlinespace[0.1em]"

    // Local Ideological Measures
    noisily display "State Ballot Measures  & " %5.3f `b_1_5' "  & " %5.3f `b_2_5' "  & " %5.3f `b_3_5' "   & " %5.3f `b_4_5' "  & " %5.3f `b_5_5' " \\"
    noisily display " $\times$ Democrat          & (" %5.3f `s_1_5' ") & (" %5.3f `s_2_5' ") & (" %5.3f `s_3_5' ") & (" %5.3f `s_4_5' ") & (" %5.3f `s_5_5' ") \\\\ [.02in]"
    noisily display "\addlinespace[0.1em]"

    // Local Spending Measures
	noisily display "Local Ballot Measures   & " %5.3f `b_1_6' " & " %5.3f `b_2_6' " & " %5.3f `b_3_6' " & " %5.3f `b_4_6' " & " %5.3f `b_5_6' "  \\"
    noisily display " $\times$ Democrat       & (" %5.3f `s_1_6' ") & (" %5.3f `s_2_6' ") & (" %5.3f `s_3_6' ") & (" %5.3f `s_4_6' ") & (" %5.3f `s_5_6' ")  \\\\ [.02in]"
    noisily display "\addlinespace[0.1em]"
	
	// Partisan Slant 
    noisily display "Partisan Slant        &        & " %5.3f `b_2_7' "     &       &      & " %5.3f `b_5_7' "  \\"
    noisily display "    $\times$ Democrat  &      & (" %5.3f `s_2_7' ") &      &     & (" %5.3f `s_5_7' ") \\\\ [.02in]"
    noisily display "\addlinespace[0.1em]"


    // Partisan Percent Difference
    noisily display "Voteshare Margin       &      &    &  " %5.3f `b_3_7' "   &   & " %5.3f `b_5_8' "  \\"
    noisily display "    $\times$ Democrat  &      &    &  (" %5.3f `s_3_7' ")  &  & (" %5.3f `s_5_8' ") \\\\ [.02in]"
    noisily display "\addlinespace[0.1em]"

    // Race Size 
    noisily display "Size                   &      &     &     & " %5.3f `b_4_7' " & " %5.3f `b_5_9' " \\"
    noisily display "    $\times$ Democrat  &      &     &   &  (" %5.3f `s_4_7' ") & (" %5.3f `s_5_9' ")   \\\\ [.02in]"
    noisily display "\addlinespace[0.1em]"
	

    noisily display "\midrule"
    noisily display "Observations            & " %12.0fc `n_1' " & " %12.0fc `n_2' " & " %12.0fc `n_3' " & " %12.0fc `n_4' " & " %12.0fc `n_5' " \\\\ [.02in]"

    noisily display "\bottomrule"
    noisily display "\end{tabular}"

    noisily display "\begin{tablenotes}"
	  noisily display "\footnotesize \item The outcome is voting for the Democrat-preferred alternative in contested candidate races or ballot measures."
	  noisily display "\footnotesize \item The coefficient on the interactions with (national) Democrat indicates the difference in APG."
    noisily display "\footnotesize \item Standard errors, clustered by contests, are in parentheses. Fixed effects for contests included in all models."
    noisily display "\end{tablenotes}"

    noisily display "\end{threeparttable}"
    noisily display "\end{table}"

    log close
}
display "`c(current_time)' `c(current_date)'"
  

